package com.best.onegous;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.best.onegous.Adapter.HomeAdapter;
import com.best.onegous.Adapter.JieXiaoAdapter;
import com.best.onegous.Adapter.ShaiDanAdapter;
import com.best.onegous.Refresh.XListView;
import com.best.onegous.Refresh.XListView.IXListViewListener;
import com.best.onegous.agreement.ApiInterface;
import com.best.onegous.agreement.Apps;
import com.best.onegous.agreement.Common;
import com.best.onegous.http.BusinessResponse;
import com.best.onegous.http.HttpClients;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.ViewGroup;

public class Shopping extends Fragment implements IXListViewListener,BusinessResponse{

	private XListView mListView;
	private List<JSONObject> mDatas = new ArrayList<JSONObject>();
	private ShaiDanAdapter mAdapter;
	private HttpClients httpclient;
	private int page = 1;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_shopping, container, false);
		mListView = (XListView) view.findViewById(R.id.listview);

		mAdapter = new ShaiDanAdapter(getActivity(), mDatas);
		mListView.setAdapter(mAdapter);
		mListView.setPullRefreshEnable(true);
		mListView.setPullLoadEnable(true);
		mListView.setXListViewListener(this);
		httpclient = new HttpClients(getActivity());
		httpclient.addResponseListener(this);
		Load();
		return view;
	}
	private void Load() {
		
		  RequestParams params = new RequestParams();
		  params.add("p",page+"");
		httpclient.httpclient(ApiInterface.YONGHUSHAIDAN, params, true);
	}
	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		page ++;
		Load();

	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		page = 1;
		Load();
		/*Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				mListView.stopRefresh();
			}
		}, 2000);*/
	}
	@Override
	public void OnMessageResponse(String url, JSONObject jo)
			throws JSONException {
		// TODO Auto-generated method stub
		
			if (url.equals(ApiInterface.YONGHUSHAIDAN)) {
				if(1==page){
					mListView.stopRefresh();
					mDatas.clear();
				}else{
					mListView.stopLoadMore();
				}
				if(null!=jo){
					JSONArray data = jo.getJSONArray("data");
					
					for (int i = 0, s = data.length(); i < s; i++) {
						mDatas.add(data.getJSONObject(i));
					}
					mAdapter.notifyDataSetChanged();
				}
			
			
		}
	}
}
